package com.ruoyi.quartz.thread;

import com.ruoyi.bizsys.domain.CreditAccount;
import com.ruoyi.business.service.IBRepaymentService;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;

@Slf4j
public class BatchRepaymentThread implements Callable {

    private String threadName;

    private Date batchDate;

    private String redisName;
    private int type;

    //private int startLimit = 0;

    //private int stepLimit = 0;

    private IBRepaymentService repaymentService;

    private RedisCache redisCache;

    public BatchRepaymentThread(String threadName, Date batchDate, String redisName, IBRepaymentService repaymentService, RedisCache redisCache,int type) {
        super();
        this.threadName = threadName;
        this.batchDate = batchDate;
        this.redisCache = redisCache;
        this.redisName = redisName;
        //this.startLimit = startLimit;
        //this.stepLimit = stepLimit;
        this.repaymentService = repaymentService;
        this.type = type;
    }

    @Override
    public String toString() {
        return "BatchRepaymentThread [threadName=" + threadName + ", batchDate="
                + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, this.batchDate)
                + ", redisName=" + redisName + "]";
    }

    @Override
    public Object call() throws Exception {
        String result = ":success";
        log.info(this.threadName + "=======[START]===" + this.toString());

        //List<CreditAccount> accountList = repaymentService.getBatchRepaymentAccount(this.startLimit, this.stepLimit);

        List<CreditAccount> accountList = redisCache.getCacheList(this.redisName);
        log.info("共有{}个客户需要执行", accountList.size());

        for (CreditAccount account : accountList) {
            log.info("客户号{}开始执行批扣操作", account.getCustNo());

            //执行日切任务
            repaymentService.doBatchRepaymentByAccount(account,type);

            log.info("客户号{}执行批扣操作正常结束", account.getCustNo());
        }

        redisCache.deleteObject(this.redisName);

        log.info(this.threadName + "=======[END]===run result:" + result);
        return threadName + result;
    }
}